package com.regulareport.common.utils.poi;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;

public class PoiUtil {
    public static String indicatorFormatter = "%s_%s_%s";
    public static String indicatorNameFormatter = "%s_%s";

    public static String getCellValueAsString(Cell cell) {
        if (cell == null) return "";

        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                } else {
                    // 避免科学计数法
                    double num = cell.getNumericCellValue();
                    if (num == (long) num) {
                        return String.valueOf((long) num);
                    } else {
                        return String.valueOf(num);
                    }
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                try {
                    return String.valueOf(cell.getNumericCellValue());
                } catch (Exception e) {
                    try {
                        return cell.getStringCellValue();
                    } catch (Exception ex) {
                        return cell.getCellFormula();
                    }
                }
            case BLANK:
                return "";
            default:
                return "";
        }
    }

    /**
     * 拆分序号和标题
     *
     * @param input
     * @return
     */
    public static String[] exactSplit(String title) {

        title = title.trim();
        // 查找序号和标题的精确边界
        int splitIndex = -1;

        // 情况1：有空格的情况
        int spaceIndex = title.indexOf(' ');
        if (spaceIndex > 0) {
            splitIndex = spaceIndex;
        }

        // 情况2：没有空格，但有序号模式（数字+点）
        if (splitIndex == -1) {
            for (int i = 0; i < title.length(); i++) {
                char c = title.charAt(i);
                // 当遇到第一个中文字符或非序号字符时，认为是标题开始
                if (isChineseChar(c) || (i > 0 && !isNumberChar(title.charAt(i - 1)) && !isNumberChar(c))) {
                    splitIndex = i;
                    break;
                }
            }
        }

        if (splitIndex > 0) {
            String number = title.substring(0, splitIndex).trim();
            String content = title.substring(splitIndex).trim();
            return new String[]{number, content};
        }

        return null;
    }

    private static boolean isNumberChar(char c) {
        return Character.isDigit(c) || c == '.' || Character.isLetter(c);
    }

    private static boolean isChineseChar(char c) {
        return c >= '\u4e00' && c <= '\u9fff';
    }

    public static void main(String[] args) {
        String[] strings = PoiUtil.exactSplit("Ⅰ. 资产");
        for (String string : strings) {
            System.out.println(string);
        }
    }
}
